// subject.js
var api = require('../../../../service/api');
var app = getApp();
var apiUrl = app.apiUrl;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    subject: {},
    isTrue:true,
    answer:'A',
    checked:true,
    answerA: '',
    answerB: '',
    answerC: '',
    answerD: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var Id = options.id;
    console.log("---id:"+Id);
    var that = this;
    var url = app.apiUrl.subject.edit + '?id=' + Id;
    api.doget(url, function (data) {
      if (data != null) {
        if (data.resultCode == 111) {
          var datas = data.resultData;
          that.setData({
            subject: datas,
          });
        }
      } else {
        wx.navigateTo({ url: '../../error/error' })
      }
    });
  },
  /**
   * 操作收藏
   */
  editCollect: function (e) {
    var that = this;
    var id = parseInt(e.currentTarget.dataset.index);
    var source = app.data.userInfo.nickName;
    var url = app.apiUrl.collect.del;
    var parems = {
      url: url,
      data: {
        id: '',
        userName: source,
        subjectId: id
      }
    }
    api.post(parems, function (data) {
      if (data != null) {
        if (data.resultCode == 111) {
          wx.showToast({
            title: '操作成功',
            icon: 'success',
            duration: 3000
          })
          wx.navigateBack({
            delta: 1
          })
        }
      } else {
        wx.navigateTo({ url: '../../error/error' })
      }
    })
  },
  radioChange: function (e) {
    var that = this;
    if (this.data.checked){
      var i = that.data.index;
      if (e.detail.value != that.data.chooseSubjects[i].answer) {
        that.setData({
          isTrue: false,
          checked:false
        });
      } else {
        that.setData({
          isTrue: true,
          checked: false
        });
      }
    }
  },
  /**
   * 检查答案
   */
  answerCheck: function (e) {
    var that = this;
    if (this.data.checked) {
      var index = parseInt(e.currentTarget.dataset.index);
      var answer='';
      switch(index){
        case 1:{
          answer='A';
        }break;
        case 2: {
          answer = 'B';
        } break;
        case 3: {
          answer = 'C';
        } break;
        case 4: {
          answer = 'D';
        } break;
      }
      that.setData({
        answer:answer
      })
      if (answer == that.data.subject.answer) {
        switch (index) {
          case 1: {
            that.setData({
              isTrue: true,
              checked: false,
              answerA: 'answer_true',
              answerB: '',
              answerC: '',
              answerD: ''
            });
          } break;
          case 2: {
            that.setData({
              isTrue: true,
              checked: false,
              answerA: '',
              answerB: 'answer_true',
              answerC: '',
              answerD: ''
            });
          } break;
          case 3: {
            that.setData({
              isTrue: true,
              checked: false,
              answerA: '',
              answerB: '',
              answerC: 'answer_true',
              answerD: ''
            });
          } break;
          case 4: {
            that.setData({
              isTrue: true,
              checked: false,
              answerA: '',
              answerB: '',
              answerC: '',
              answerD: 'answer_true'
            });
          } break;
        }
      } else {
        switch (index) {
          case 1: {
            that.setData({
              isTrue: false,
              checked: false,
              answerA: 'answer_false',
              answerB: '',
              answerC: '',
              answerD: ''
            });
          } break;
          case 2: {
            that.setData({
              isTrue: false,
              checked: false,
              answerA: '',
              answerB: 'answer_false',
              answerC: '',
              answerD: ''
            });
          } break;
          case 3: {
            that.setData({
              isTrue: false,
              checked: false,
              answerA: '',
              answerB: '',
              answerC: 'answer_false',
              answerD: ''
            });
          } break;
          case 4: {
            that.setData({
              isTrue: false,
              checked: false,
              answerA: '',
              answerB: '',
              answerC: '',
              answerD: 'answer_false'
            });
          } break;
        }
      }
    }
  }
})